package com.module.imagelib.glide.transfor;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

import java.security.MessageDigest;

import androidx.annotation.Nullable;

/**
 * 圆形图片
 * Created by dell on 2017/10/25
 */

public class GlideCircleTransform extends BitmapTransformation {

    protected int mBorderWidth = 0;
    protected int mBorderColor = Color.parseColor("#000000");
    private static String ID = "com.kangcun.imageloader.glide.transfor.GlideCircleTransform";
    private static final byte[] ID_BYTES = ID.getBytes(CHARSET);

    public GlideCircleTransform() {
    }

    public GlideCircleTransform(int width, String color) {
        this.mBorderWidth = width;
        this.mBorderColor = Color.parseColor(color);
    }

    @Override
    public Bitmap transform(BitmapPool pool, Bitmap toTransform,
                            int outWidth, int outHeight) {
        return circleCrop(pool, toTransform);
    }

    private Bitmap circleCrop(BitmapPool pool, Bitmap source) {
        //override(500, 300)与override(300, 500)
        //返回541-300与901-500
        //画圆形图
        if (source == null) return null;
        int size = Math.min(source.getWidth(), source.getHeight());
        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;
        // TODO this could be acquired from the pool too
        //原图转成指定宽高矩形切割图
        Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
        //创建一个空bitmap
        Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        }
        //创建空画布
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        //矩形图配合圆生成圆形图
        paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        float r = size / 2f;

        RectF srcRectF = new RectF(0, 0, size, size);
        int sr = 0;
        //缩小canvas，避免边框遮盖合成图片
        if (mBorderWidth > 0) {
            sr = canvas.saveLayer(srcRectF, null, Canvas.ALL_SAVE_FLAG);
            float sx = 1.0f * (srcRectF.width() - 2 * mBorderWidth) / srcRectF.width();
            float sy = 1.0f * (srcRectF.height() - 2 * mBorderWidth) / srcRectF.height();
            canvas.scale(sx, sy, srcRectF.width() / 2, srcRectF.height() / 2);
        }
        //原图
        canvas.drawCircle(r, r, r, paint);
        //画边框
        if (mBorderWidth > 0) {
            //恢复画布
            canvas.restoreToCount(sr);
            RectF borderRectF = new RectF(mBorderWidth / 2.0f, mBorderWidth / 2.0f, srcRectF.width() - mBorderWidth / 2.0f, srcRectF.height() - mBorderWidth / 2.0f);
            drawBorder(canvas, borderRectF);
        }

        return result;
    }

    /**
     * 必须加-防止闪烁
     * @param messageDigest
     */
    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {
        messageDigest.update(ID_BYTES);
    }

    /**
     * 必须加-防止闪烁
     * @param obj
     */
    @Override
    public boolean equals(@Nullable Object obj) {
        return obj instanceof GlideCircleTransform;
    }

    /**
     * 必须加-防止闪烁
     */
    @Override
    public int hashCode() {
        return ID.hashCode();
    }

    public void drawBorder(Canvas canvas, RectF borderRectF) {
        Path mPath = new Path();
        Paint mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(mBorderColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mBorderWidth);
        mPath.addCircle(borderRectF.centerX(), borderRectF.centerY(), borderRectF.width() / 2f, Path.Direction.CW);
        canvas.drawPath(mPath, mPaint);
    }

}